<?php
namespace Qy {
    /**
    * 企业微信认证接口类
    * @author Jamers
    * @since 2017.1.1
    * @see http://qydev.weixin.qq.com/wiki/index.php?title=%E8%BA%AB%E4%BB%BD%E9%AA%8C%E8%AF%81
    * @since 含认证接口中所有功能
    */
    class Oauth {
        public $corpid, $corpsecret, $a_token;
        private $common;
        public function __construct($ary = array('corpid'=>'','corpsecret'=>'','accesstoken'=>'')) {
            $a_token = '';
            if ($ary) {
                if (property_exists(__CLASS__,'corpid')) if (isset($ary['corpid']) && $ary['corpid']) $this->corpid = $ary['corpid'];
                if (property_exists(__CLASS__,'corpsecret')) if (isset($ary['corpsecret']) && $ary['corpsecret']) $this->corpsecret = $ary['corpsecret'];
                if (property_exists(__CLASS__,'a_token')) if (isset($ary['accesstoken']) && $ary['accesstoken']) $this->a_token = $ary['accesstoken'];
            }
            if (isset($ary['common'])) {
                $this->common = &$ary['common'];
            }else{
                $this->common = new \Qy\Common();
            }
            if (empty($this->a_token)) {
                if ($a_token) {
                    $this->a_token = $a_token;
                }else{
                    $this->a_token = $this->common->getAccessToken($this->corpid,$this->corpsecret);
                }
            }
            //if (empty($this->a_token)) throw new Exception('AccessToken is empty!');
        }
        
        /**
        * 生成换取用户CODE链接函数
        * @see http://qydev.weixin.qq.com/wiki/index.php?title=OAuth%E9%AA%8C%E8%AF%81%E6%8E%A5%E5%8F%A3
        * 
        * @param mixed $redirect_uri
        * @param mixed $corpid
        * @param mixed $state
        * @return mixed
        */
        public function getOauthCodeUrl($redirect_uri,$corpid='',$state='') {
            if (empty($redirect_uri)) return '';
            if (empty($corpid)) $corpid = $this->corpid;
            $uri = urlencode($redirect_uri);
            $url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid={$corpid}&redirect_uri={$uri}&response_type=code&scope=SCOPE&state={$state}#wechat_redirect";
            return $url;
        }
        
        /**
        * 根据code获取成员信息
        * @see http://qydev.weixin.qq.com/wiki/index.php?title=OAuth%E9%AA%8C%E8%AF%81%E6%8E%A5%E5%8F%A3
        * 
        * @param mixed $code
        * @param AccessToken $access_token
        */
        public function getUserinfoFromCode($code,$access_token='') {
            if (empty($access_token)) $access_token = $this->a_token;
            $url = "https://qyapi.weixin.qq.com/cgi-bin/user/getuserinfo?access_token={$access_token}&code={$code}";
            $ret = $this->common->httpRequest($url);
            return $ret;
        }
        
        /**
        * userid转换成openid接口
        * @see http://qydev.weixin.qq.com/wiki/index.php?title=Userid%E4%B8%8Eopenid%E4%BA%92%E6%8D%A2%E6%8E%A5%E5%8F%A3
        * 
        * @param mixed $ary
        * @param AccessToken $a_token
        * @return mixed
        */
        public function userid2openid($ary,$a_token='') {
            if (is_string($ary)) {
                $post = array('userid' => $ary);
            }else if (is_array($ary)) {
                $post = $ary;
            }else{
                return '';
            }
            if (empty($a_token)) $a_token = $this->a_token;
            $url = "https://qyapi.weixin.qq.com/cgi-bin/user/convert_to_openid?access_token={$a_token}";
            $ret = $this->common->httpRequest($url,$post,'post');
            return $ret;
        }
        
        /**
        * openid转换成userid接口
        * @see http://qydev.weixin.qq.com/wiki/index.php?title=Userid%E4%B8%8Eopenid%E4%BA%92%E6%8D%A2%E6%8E%A5%E5%8F%A3
        * 
        * @param mixed $ary
        * @param AccessToken $a_token
        * @return mixed
        */
        public function openid2userid($ary,$a_token='') {
            if (is_string($ary)) {
                $post = array('openid' => $ary);
            }else if (is_array($ary)) {
                $post = $ary;
            }else{
                return '';
            }
            if (empty($a_token)) {
                $a_token = $this->a_token;
            }
            $url = "https://qyapi.weixin.qq.com/cgi-bin/user/convert_to_userid?access_token={$a_token}";
            $ret = $this->common->httpRequest($url,$post,'post');
            return $ret;
        }
        
        /**
        * 生成登录授权页链接
        * @see http://qydev.weixin.qq.com/wiki/index.php?title=%E6%88%90%E5%91%98%E7%99%BB%E5%BD%95%E6%8E%88%E6%9D%83
        * 
        * @param mixed $redirect_uri
        * @param mixed $type
        * @param mixed $state
        * @param mixed $corpid
        * @return mixed
        */
        public function getOauthLoginPageUrl($redirect_uri,$type='admin',$state='',$corpid='') {
            if (empty($redirect_uri)) return '';
            if (empty($corpid)) $corpid = $this->corpid;
            $uri = urlencode($redirect_uri);
            $typeary = array('member','admin','all');
            if (!in_array($type,$typeary)) $type='';
            $url = "https://qy.weixin.qq.com/cgi-bin/loginpage?corp_id={$corpid}&redirect_uri={$uri}&state={$state}&usertype={$type}";
            return $url;
        }
        
        /**
        * 获取企业号登录用户信息
        * @see http://qydev.weixin.qq.com/wiki/index.php?title=%E6%88%90%E5%91%98%E7%99%BB%E5%BD%95%E6%8E%88%E6%9D%83
        * 
        * @param mixed $ary
        * @param AccessToken $a_token
        * @return mixed
        */
        public function get_login_info($ary,$a_token='') {
            if (is_string($ary)) {
                $post = array('auth_code' => $ary);
            }else if (is_array($ary)) {
                $post = $ary;
            }else{
                return '';
            }
            if (empty($a_token)) $a_token = $this->a_token;
            $url = "https://qyapi.weixin.qq.com/cgi-bin/service/get_login_info?access_token={$a_token}";
            $ret = $this->common->httpRequest($url,$post,'post');
            return $ret;
        }
        
        /**
        * 获取登录企业号官网的url
        * @see http://qydev.weixin.qq.com/wiki/index.php?title=%E5%8D%95%E7%82%B9%E7%99%BB%E5%BD%95%E6%8E%88%E6%9D%83
        * 
        * @param mixed $ary
        * @param AccessToken $a_token
        * @return mixed
        */
        public function get_login_url($ary,$a_token='') {
            if (!is_array($ary)) return '';
            if (empty($a_token)) $a_token = $this->a_token;
            $url = "https://qyapi.weixin.qq.com/cgi-bin/service/get_login_url?access_token={$a_token}";
            $ret = $this->common->httpRequest($url,$ary,'post');
            return $ret;
        }
    
    }
}
?>
